package com.devilvan.betrice.util;

import cn.hutool.core.util.StrUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.http.Consts;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author Evad.Wu
 * @Description 字符串相关 工具类
 * @date 2022-09-25
 */
@SuppressWarnings(value = "unused")
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UString extends StrUtil {
    /**
     * 通过 {@link UriComponentsBuilder} 生成一个带参数的get请求URL
     *
     * @param url    原字符串
     * @param params get请求参数
     * @return 带参数的get请求URL
     */
    public static String generateHttpParamUrlByUriBuilder(String url, Map<String, Object> params) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url);
        params.forEach(uriComponentsBuilder::queryParam);
        return uriComponentsBuilder.encode().toUriString();
    }

    /**
     * 生成一个带参数的get请求URL
     *
     * @param url    原字符串
     * @param params get请求参数
     * @return 带参数的get请求URL
     */
    public static String generateHttpParamUrl(String url, Map<String, Object> params) {
        List<BasicNameValuePair> basicNameValuePairList = new ArrayList<>();
        Set<Map.Entry<String, Object>> entrySet = params.entrySet();
        for (Map.Entry<String, Object> entry : entrySet) {
            basicNameValuePairList.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
        }
        //转换为键值对
        String str = null;
        try {
            str = url + "?" + EntityUtils.toString(new UrlEncodedFormEntity(basicNameValuePairList, Consts.UTF_8.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 将字符串首字母大写
     *
     * @param str 待转化的字符串
     * @return 首字母大写后的字符串
     */
    public static String upperCaseFirst(String str) {
        if (UEmpty.isNotEmpty(str)) {
            String tmp = str.substring(0, 1).toUpperCase(Locale.ROOT);
            return tmp + str.substring(1);
        }
        return str;
    }

    /**
     * URLEncode格式字符串转UTR-8字符串
     *
     * @param urlEncodeStr 待转换的字符串
     * @return 转换后的字符串
     */
    public static String decode(String urlEncodeStr) {
        return URLDecoder.decode(urlEncodeStr, StandardCharsets.UTF_8);
    }

    /**
     * 集合转字符串
     *
     * @param list     集合
     * @param separate 分隔符
     * @return 拼接后的字符串
     */
    public static String join(List<String> list, String separate) {
        if (UEmpty.isEmpty(list)) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (String s : list) {
            sb.append(s).append(separate);
        }
        String string = sb.toString();
        return string.substring(0, string.length() - 1);
    }

    /**
     * 数组转字符串
     *
     * @param arr      数组
     * @param separate 分隔符
     * @return 拼接后的字符串
     */
    public static String join(String[] arr, String separate) {
        if (UEmpty.isEmpty(arr)) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (String s : arr) {
            sb.append(s).append(separate);
        }
        String string = sb.toString();
        return string.substring(0, string.length() - 1);
    }
}
